layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function(){
    var form = layui.form,
        table=layui.table,
        admin  = layui.admin;
    table.render({
        elem: '#enterprise_Terminal_table'
        ,url: '/agency_terminal/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'id', title: 'ID',width:100, "align":"center"}
            ,{field:'sn',width:200, title: '设备序号',"align":"center"}
            ,{field:'userId',width:200, title: '所属商家ID',"align":"center"}
            ,{field:'createdTime',width:200, title: '创建时间',"align":"center"}
        ]]
        ,request:{
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where:{}
        ,id: 'enterprise_Terminal_table'
        ,page: true
    });


    table.on('tool(enterprise_Terminal_table)', function(obj){
        var table_event = obj.event;

        var data = obj.data;

        if(table_event=="modify_enterpriseTerminal"){

            admin.popup({
                title: '编辑'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '500px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#add_enterpriseTerminal_win")
                ,end:function () {
                    table.reload('enterprise_Terminal_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                        }
                    });
                }
            });

            form.val('add_enterpriseTerminal_form', {
                "id":data.id,
                "code":data.code,
                "imei":data.imei
            });

        }else if(table_event=='bing_store'){

            initStoreSelect();

            setTimeout(function () {
              admin.popup({
                  title: '编辑'
                  ,shade: 0
                  ,anim: -1
                  ,offset:"20px"
                  ,area: ['80%', '500px']
                  ,skin: 'layui-anim layui-anim-upbit'
                  ,content:$("#bing_store_win")
                  ,end:function () {
                      table.reload('enterprise_Terminal_table', {
                          page: {
                              curr: 1 //重新从第 1 页开始
                          }
                          ,where: {

                          }
                      });
                  }
              });

              form.val('bing_store_form', {
                  "id":data.id,
                  "storeId":data.storeId
              })
          },200);
        }

    });

    //添加设备
    $(".enterpriseTerminalAdd_btn").click(function(){

        $("#add_enterpriseTerminal_form")[0].reset();
        /**
         * 初始化 等级下拉
         */

        admin.popup({
            title: '编辑'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['80%', '500px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#add_enterpriseTerminal_win")
            ,end:function () {
                table.reload('enterprise_Terminal_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {

                    }
                });
            }
        });
        form.val('add_enterpriseTerminal_form', {
            "id":""
        });

    });


    form.on('submit(addenterpriseTerminalOk)',function (data) {

        $.ajax({
            url:"/enterprise_terminal/edit",
            data:data.field,
            type:"post",
            success:function (result) {
                if (result.code == 1){
                    layer.msg("编辑成功",{
                        offset: '32%'
                        ,icon: 1
                        ,time: 0
                        ,btn: '知道了'
                        ,btnAlign:"c"
                        ,shade:0.4
                        ,yes:function () {
                            table.reload('enterprise_Terminal_table', {
                            });
                            layer.closeAll();
                        }
                    })
                }else{
                    layer.msg(result.message,{
                        offset: '32%'
                        ,icon: 2
                        ,time: 3000
                    },function () {
                        layer.closeAll();
                    })
                }

            }
        });
        return false;
    });

    form.on('submit(bingStotreOk)',function (data) {

        $.ajax({
            url:"/enterprise_terminal/storeBing",
            data:data.field,
            type:"post",
            success:function (result) {
                if (result.code == 1){
                    layer.msg("绑定成功",{
                        offset: '32%'
                        ,icon: 1
                        ,time: 0
                        ,btn: '知道了'
                        ,btnAlign:"c"
                        ,shade:0.4
                        ,yes:function () {
                            table.reload('enterprise_Terminal_table', {
                            });
                            layer.closeAll();
                        }
                    })
                }else{
                    layer.msg(result.message,{
                        offset: '32%'
                        ,icon: 2
                        ,time: 3000
                    },function () {
                        layer.closeAll();
                    })
                }

            }
        });
        return false;
    });

    $(".search_btn").click(function () {
        table.reload('enterprise_Terminal_table', {
            where:{
                storeName:$(".queryTable input[name='storeName']").val()
            }
        });
    });


});


/**
 * 初始化门店下拉列表
 */
function initStoreSelect() {
    $.post(
        "/enterprise_store/getStores",
        {},
        function (result) {

            if(result.code==1){

                $('#bing_store_form select[name="storeId"] option').remove();
                $.each(result.data, function (index, item) {
                 var s =   $('#bing_store_form select[name="storeId"]');
                   s.append(new Option(item.name, item.id));// 下拉菜单里添加元素
                });

            }else {
                layer.msg(result.message,{
                    offset: '32%'
                    ,icon: 2
                    ,time: 3000
                },function () {
                    layer.closeAll();
                })
            }
        }
    );
};


